package com.leantech.android.browser.ui.search.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.cscj.android.repository.db.entity.SearchHistoryEntity

import com.leantech.android.browser.databinding.ItemSearchHistoryBinding

import com.leantech.android.browser.utils.isVisible
import com.leantech.android.browser.utils.onThrottleClick
import com.leantech.android.browser.utils.show

class SearchHistoryAdapter(private val callback: SearchHistoryItemCallback) :
    ListAdapter<SearchHistoryEntity, SearchHistoryAdapter.SearchHistoryViewHolder>(
        diffCallback
    ) {

    private var show = false
    fun show(show: Boolean) {
        this.show = show
        notifyItemRangeChanged(0, itemCount, PayloadToggleEdit)
    }


    companion object {
        private const val PayloadToggleEdit = 1

        val diffCallback = object : DiffUtil.ItemCallback<SearchHistoryEntity>() {
            override fun areItemsTheSame(
                oldItem: SearchHistoryEntity,
                newItem: SearchHistoryEntity
            ): Boolean {
                return oldItem.key == newItem.key
            }

            override fun areContentsTheSame(
                oldItem: SearchHistoryEntity,
                newItem: SearchHistoryEntity
            ): Boolean {
                return oldItem == newItem
            }
        }
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchHistoryViewHolder {
        val binding =
            ItemSearchHistoryBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return SearchHistoryViewHolder(binding)
    }

    override fun onBindViewHolder(holder: SearchHistoryViewHolder, position: Int) {
        holder.bind(getItem(position), callback, show)
    }

    override fun onBindViewHolder(
        holder: SearchHistoryViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            payloads.forEach { payload ->
                when (payload) {
                    PayloadToggleEdit -> {
                        holder.enableEdit(show)
                    }

                    else -> {
                        super.onBindViewHolder(holder, position, payloads)
                    }
                }

            }
        }

    }

    class SearchHistoryViewHolder(private val binding: ItemSearchHistoryBinding) :
        ViewHolder(binding.root) {

        fun bind(
            item: SearchHistoryEntity,
            callback: SearchHistoryItemCallback,
            show: Boolean
        ) {

            item.apply {
                binding.title.text = key

            }
            binding.btnClose.onThrottleClick {
                if (binding.btnClose.isVisible()) {
                    callback.onHistoryItemRemoved(item, bindingAdapterPosition)
                }

            }
            enableEdit(show)
            binding.root.setOnLongClickListener {
                true
            }

            binding.root.onThrottleClick {
                callback.onHistoryItemClicked(item.key)
            }

        }

        fun enableEdit(enable: Boolean) {
            binding.btnClose.show(enable)
        }

    }


    interface SearchHistoryItemCallback {

        fun onHistoryItemClicked(searchKeywords: String)

        fun onHistoryItemRemoved(item: SearchHistoryEntity, adapterPosition: Int)
    }
}